﻿using System.IO;
using System.IO.Compression;

namespace PKTools
{
    public class Compress
    {
        const int
            bufLength = 1024 * 20;  // compress data by chunks of this size [bytes]

        public static byte[] Encode(byte[] data)
        {
            using (var ms = new MemoryStream())
                using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
                {
                    zip.Write(data, 0, data.Length);
                    zip.Close();

                    return ms.ToArray();
                }
        }

        public static byte[] Decode(byte[] input)
        {
            var
                temp = new byte[bufLength];
            int
                n;

            using (var ms = new MemoryStream(input))
                using (var zip = new GZipStream(ms, CompressionMode.Decompress))
                    using (var output = new MemoryStream())
                    {
                        while ((n = zip.Read(temp, 0, bufLength)) != 0)
                            output.Write(temp, 0, n);

                        zip.Close();

                        return output.ToArray();
                    }                    
            }
    }
}
